<?php

	namespace application\controller;

		require_once ("application/controller/AdminController.php");
		require_once ("application/view/RecipeCollectionView.php");
		require_once ("application/model/RecipeCollectionModel.php");
		require_once ("application/model/RecipeDALmodel.php");

		class ApplicationController
		{

			private $recipeCollectionView;
			private $recipeCollection;
			private $message;
			private $recipe;
			private $recipeId;
			private $searchType;
			private $searchAuthor;

			//tar in en recipeCollectionLista och skickar den vidare till vyn
			public function __construct(\application\model\RecipeCollectionModel $recipeCollection)
			{
				$this->recipeCollection = $recipeCollection;
				$this->recipeCollectionView = new \application\view\RecipeCollectionView($this->recipeCollection);
				$this->loginController = new \login\controller\LoginController();
				$this->adminController = new \application\controller\AdminController($this->recipeCollection);
			}


			public function viewPage()
			{
				//LÄSA
				if ($this->recipeCollectionView->userWantsToReadRecipeCollectionList() == true)
				{

					return $this->recipeCollectionView->getRecipeCollectionListHTML();
				}

				if ($this->recipeCollectionView->userWantsToReadRecipe() == true)
				{
					try
					{
						$recipeId = $this->recipeCollectionView->userWantsToReadThisRecipe();
						$recipe = $this->recipeCollection->getSingleRecipe($recipeId);
					}
					catch (Exception $exception)
					{
						$this->message = $e->getMessage();
					}
					return $this->recipeCollectionView->getFullRecipeHTML($recipe);
				}
				
				
				if ($this->recipeCollectionView->userWantsToSearchRecipes() == true)
				{
					return $this->recipeCollectionView->ViewRecipeSearchPage();
				}
				
				//SEARCH////////////////
				if ($this->recipeCollectionView->userWantsToSearchRecipeType() == true)
				{
					$this->searchType = $this->recipeCollectionView->userWantsToSearchThisType();
					$this->recipeCollection = $this->recipeCollection->getRecipeListByType($this->searchType);
					return $this->recipeCollectionView->getRecipeCollectionListRequested($this->recipeCollection);
				}
				
				if ($this->recipeCollectionView->userWantsToSearchRecipeAuthor() == true)
				{
					$this->searchAuthor = $this->recipeCollectionView->userWantsToSearchThisAuthor();
					$this->recipeCollection = $this->recipeCollection->getRecipeListByAuthor($this->searchAuthor);
					
					return $this->recipeCollectionView->getRecipeCollectionListRequested($this->recipeCollection);
				}
				
				if ($this->recipeCollectionView->userWantsToSearchThisText() == true)
				{
					$this->searchText = $this->recipeCollectionView->userWantsToSearchThisText();
					$this->recipeCollection = $this->recipeCollection->getRecipeListByText($this->searchText);
					
					return $this->recipeCollectionView->getRecipeCollectionListRequested($this->recipeCollection);
				}
				
				if ($this->recipeCollectionView->userWantsToSearchThisTag() == true)
				{
					$this->searchTag = $this->recipeCollectionView->userWantsToSearchThisTag();
					$this->recipeCollection = $this->recipeCollection->getRecipeListByTag($this->searchTag);
					
					return $this->recipeCollectionView->getRecipeCollectionListRequested($this->recipeCollection);
				}
				
				if ($this->loginController->isUserLoggedIn() == true)
				{
					return $this->adminController->viewAdminPage();
				}
				else
				{
					return $this->recipeCollectionView->WorkInAdminSectionFail();
				}

				return $this->recipeCollectionView->getRecipeCollectionListHTML();
			}
		}




















